/**
 * 支付相关服务
 */

const util = require('../utils/util.js');
const API = require('../config/api.js');
function payResult(paylogId) {
  util.request(
    API.PayResult,
    {
      paylog_id: paylogId
    }
  ).then(res => {
    console.log('好', res);
  }).catch(error => {
    console.log('异常', error);
  });
}
/**
 * 钱包充值
 */
function addCredit(price) {
  return new Promise(function (resolve, reject) {
    util.request(API.AddCredit, {
      price: price,
      type: 33
    }).then((res) => {
      if (res.result === 'success') {
        const payParam = res.data.params;
        const paylogId = res.data.paylog_id;
        console.log(payParam);
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            payResult(paylogId);
            resolve({"result":"success", "paylogId": paylogId});
            resolve(res);
          },
          'fail': function (res) {
            reject(res);
          },
          'complete': function (res) {
            reject(res);
          }
        });
      } else {
        reject(res);
      }
    });
  });
}
/**
 * 购买卡券
 */
function buyDeal(dealId, cid) {
  return new Promise(function (resolve, reject) {
    util.request(API.BuyDeal, {
      deal_id: dealId,
      coupon_id: cid,
      type: 22
    }).then((res) => {
      if (res.result === 'success') {
        const payParam = res.data.params;
        const paylogId = res.data.paylog_id;
        console.log(payParam);
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            payResult(paylogId);
            resolve({"result":"success", "paylogId": paylogId});
            resolve(res);
          },
          'fail': function (res) {
            reject(res);
          },
          'complete': function (res) {
            reject(res);
          }
        });
      } else {
        reject(res);
      }
    });
  });
}
/**
 * 支付坐席订单
 */
function payOrder(orderId) {
  return new Promise(function (resolve, reject) {
    util.request(API.Pay, {
      order_id: orderId,
      type: 11
    }).then((res) => {
      if (res.result === 'success') {
        const payParam = res.data.params;
        const paylogId = res.data.paylog_id;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (pres) {
            console.log("S", pres);
            payResult(paylogId);
            resolve({"result":"success", "paylogId": paylogId});
          },
          'fail': function (pres) {
            console.log("F",pres);
            reject(res);
          },
          'complete': function (pres) {
            console.log("C",pres);
            reject(res);
          }
        });
      } else {
        reject(res);
      }
    });
  });
}


module.exports = {
  payOrder,
  buyDeal,
  addCredit
};











